#![allow(unused)]
use futures::future::join_all;
use regex::Regex;
use scraper::{Html, Selector};
use std::error::Error;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;

const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36";
async fn fetch_path(path: &str) -> Result<String, reqwest::Error> {
    let client = reqwest::Client::builder().user_agent(USER_AGENT).build()?;
    let mut back_string = String::new();
    match client.get(path).send().await {
        Ok(response) => {
            match response.text().await {
                Ok(text) => {
                    back_string = text;
                }
                Err(_) => {
                    println!("Read response text Error!")
                }
            };
        }
        Err(_) => println!("reqwest get Error!"),
    }
    Ok(back_string)
}

async fn down_image(url: &str) -> Result<(), Box<dyn Error>> {
    println!("{}", url);
    let re = Regex::new(r"/(?P<name>\d*?\.png)").unwrap();
    let caps = re.captures(url).unwrap();
    let client = reqwest::ClientBuilder::new()
        .user_agent(USER_AGENT)
        .build()?;
    let mut res = client
        .get(url)
        .header("Referer", "https://www.amz123.com/")
        .send()
        .await?;
    // dbg!(&res);
    let bytes = res.bytes().await?;

    // dbg!(bytes);
    println!("`mkdir img`");
    // 创建一个目录，返回 `io::Result<()>`
    if let Err(why) = fs::create_dir("img") {
        println!("! {:?}", why.kind())
    }
    let path = Path::new("img").join(&caps["name"]);
    let mut f = File::create(path).expect("create error");
    f.write_all(&bytes).expect("write error");

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let paths = vec!["https://www.amz123.com/tools-timeconversion"];
    let result_list = join_all(paths.into_iter().map(|path| fetch_path(path))).await;

    let mut str1 = String::new();
    for ele in result_list.into_iter() {
        let content = ele.unwrap();
        str1.push_str(&content);
    }

    let mut url_list: Vec<String> = vec![];
    let document = Html::parse_document(&str1);
    let selector = Selector::parse(".divitem>img").unwrap();
    for element in document.select(&selector) {
        // dbg!(element.value().attr("src"));
        url_list.push(element.value().attr("src").unwrap().to_string());
    }

    for url in url_list {
        down_image(&url).await?;
    }
    // 单个测试
    // down_image("https://img.amz123.com/upload/time_icon/20210106/16098714351279.png/viewthumb").await?;
    Ok(())
}
